package logic

import (
	"campus-trade/campusrpc/campusrpc"
	"campus-trade/campusrpc/internal/svc"
	"campus-trade/models/mysql"
	"context"
	"fmt"

	"github.com/zeromicro/go-zero/core/logx"
)

type BookAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewBookAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BookAddLogic {
	return &BookAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *BookAddLogic) BookAdd(in *campusrpc.BookAddRequest) (*campusrpc.BookAddResponse, error) {
	// todo: add your logic here and delete this line

	var book mysql.Book
	if book.Name == "" {
		return nil, fmt.Errorf("商品发布时验证标题、分类、价格、描述等必填项")
	}

	if book.Title == "" {
		return nil, fmt.Errorf("商品发布时验证标题、分类、价格、描述等必填项")
	}

	if book.Price == 0 {
		return nil, fmt.Errorf("商品发布时验证标题、分类、价格、描述等必填项")
	}

	if book.Tab == "" {
		return nil, fmt.Errorf("商品发布时验证标题、分类、价格、描述等必填项")
	}

	book = mysql.Book{
		Name:    in.Name,
		Title:   in.Title,
		Price:   in.Price,
		Tab:     in.Tab,
		UserId:  in.UserId,
		ClassId: in.ClassId,
	}
	mysql.DB.Debug().Create(&book)
	return &campusrpc.BookAddResponse{
		BookId: int64(book.ID),
	}, nil
}
